Skip to content

Implement telegram integration for task creation and event extraction, plus saving to db#16

Merged
Itaypk merged 1 commit into
mainfrom
adding-telegram-input
May 24, 2026
Merged

Implement telegram integration for task creation and event extraction, plus saving to db#16
Itaypk merged 1 commit into
mainfrom
adding-telegram-input

Conversation

@orenIsabella

Copy link
Copy Markdown
Owner

No description provided.

@orenIsabella orenIsabella requested a review from Itaypk May 24, 2026 15:38
@qodo-code-review

Copy link
Copy Markdown

Review Summary by Qodo

Implement Telegram bot integration with event extraction and database persistence

✨ Enhancement

Grey Divider

Walkthroughs

Description
• Add Telegram bot integration for task creation from messages
• Create TaskInput database model to store extracted events
• Implement task_input_service for persisting data to database
• Add automatic database table creation on application startup
Diagram
flowchart LR
  User["User sends message<br/>via Telegram"]
  Bot["Telegram Bot<br/>Message Handler"]
  Extractor["Event Extractor<br/>Service"]
  DB["TaskInput<br/>Database Model"]
  Response["Send Event Draft<br/>to User"]
  
  User -->|"message_text"| Bot
  Bot -->|"extract_event_from_text"| Extractor
  Extractor -->|"extracted_event"| Bot
  Bot -->|"save_task_input"| DB
  DB -->|"persisted"| Bot
  Bot -->|"formatted response"| Response

Loading

File Changes

1. app/main.py ✨ Enhancement +6/-1

Add database initialization on startup

• Import Base from database and TaskInput model
• Add startup event handler to create database tables
• Initialize SQLAlchemy metadata on application startup

app/main.py


2. app/models/__init__.py ✨ Enhancement +3/-0

Create models package initialization

• Create new models package with TaskInput export
• Provide clean module interface for model imports

app/models/init.py


3. app/models/task_input.py ✨ Enhancement +25/-0

Define TaskInput database model

• Define TaskInput SQLAlchemy ORM model with task_inputs table
• Store source, original text, and extracted event data
• Include optional Telegram chat and message IDs for tracking
• Add created_at timestamp with UTC default

app/models/task_input.py


View more (3)
4. app/services/task_input_service.py ✨ Enhancement +28/-0

Create task input persistence service

• Implement save_task_input async function for database persistence
• Accept source, text, extracted event, and optional Telegram IDs
• Commit transaction and refresh object before returning

app/services/task_input_service.py


5. app/services/telegram_bot.py ✨ Enhancement +86/-0

Implement Telegram bot message handler

• Implement Telegram bot with start command and message handler
• Extract events from user messages using event_extractor service
• Save extracted events to database with Telegram metadata
• Format and send event draft response to user with title, times, and confidence
• Handle OpenRouterError and general exceptions with user-friendly messages

app/services/telegram_bot.py


6. .env.example ⚙️ Configuration changes +3/-1

Add Telegram environment variables

• Add TELEGRAM_BOT_TOKEN configuration variable
• Add TELEGRAM_WEBHOOK_SECRET configuration variable

.env.example


Grey Divider

Qodo Logo

@qodo-code-review

qodo-code-review Bot commented May 24, 2026

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (3) 📘 Rule violations (1)

Grey Divider


Action required

1. Missing Telegram dependency 🐞 Bug ≡ Correctness
Description
app/services/telegram_bot.py imports the telegram package, but the project dependency list does
not include python-telegram-bot, so importing/running the bot will fail with
ModuleNotFoundError. This blocks the Telegram integration from working at all.
Code

app/services/telegram_bot.py[R4-6]

Evidence
The new Telegram bot module directly imports telegram/telegram.ext, but the declared runtime
dependencies in pyproject.toml do not include python-telegram-bot, and the lockfile contains no
Telegram package entries, so the import cannot succeed in a clean install.

app/services/telegram_bot.py[1-13]
pyproject.toml[1-13]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
`app/services/telegram_bot.py` imports `telegram` / `telegram.ext`, but the repository does not declare a dependency that provides those modules. This will crash at import-time with `ModuleNotFoundError: No module named 'telegram'`.

## Issue Context
- The Telegram bot implementation lives in `app/services/telegram_bot.py`.
- Dependencies are declared in `pyproject.toml`; lockfile should be updated accordingly.

## Fix
1. Add `python-telegram-bot` to `[project].dependencies` in `pyproject.toml`.
2. Re-lock dependencies (e.g., `uv add python-telegram-bot` or equivalent) so `uv.lock` is updated.

## Fix Focus Areas
- pyproject.toml[1-13]
- app/services/telegram_bot.py[1-13]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools



Remediation recommended

2. Non-placeholder TELEGRAM_WEBHOOK_SECRET value 📘 Rule violation ⛨ Security
Description
.env.example adds TELEGRAM_WEBHOOK_SECRET=some-long-random-secret, which does not look like a
clear placeholder and risks committing a real secret to version control. This violates the
requirement that .env.example contain only non-secret placeholder/example values.
Code

.env.example[R12-13]

Evidence
PR Compliance ID 1 forbids committing real secrets and requires .env.example to contain only
placeholders. The PR adds TELEGRAM_WEBHOOK_SECRET=some-long-random-secret, which is not an
explicit placeholder pattern (e.g., your_webhook_secret_here) and may be interpreted/used as an
actual secret.

CLAUDE.md: Do Not Commit Environment-Specific Secrets; Use Gitignored .env.dev Copied From .env.example
.env.example[12-13]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
`.env.example` includes a likely non-placeholder value for `TELEGRAM_WEBHOOK_SECRET`, which may represent a real secret and should not be committed.

## Issue Context
Compliance requires `.env.example` to contain only non-secret placeholder/example values.

## Fix Focus Areas
- .env.example[12-13]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


3. Startup DDL create_all risk 🐞 Bug ☼ Reliability
Description
app/main.py executes Base.metadata.create_all() on every application startup, performing schema
DDL implicitly at runtime. This can cause startup races/locks or inconsistent schema management when
running multiple workers/replicas and makes DB changes harder to control.
Code

app/main.py[R30-33]

Evidence
The new startup handler unconditionally calls Base.metadata.create_all() using the shared engine,
meaning every app process will attempt schema creation on startup.

app/main.py[18-34]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
The app runs `Base.metadata.create_all()` during startup. Running DDL automatically at runtime is risky in multi-process/multi-replica deployments and makes schema evolution implicit.

## Issue Context
- `app/main.py` already defines a `lifespan` handler and now also defines a separate startup event that runs DDL.
- The code imports `TaskInput` solely to ensure the model is registered for `create_all()`.

## Fix
Choose one:
1. Preferred: remove the `create_all()` startup hook and manage schema changes via a dedicated migration/init step.
2. If you must keep auto-creation for local/dev: gate it behind an env flag (e.g., `AUTO_CREATE_TABLES=true`) and ensure it runs in a single controlled process (not every worker).

Also consider moving all startup logic into the existing `lifespan` context manager to avoid split initialization.

## Fix Focus Areas
- app/main.py[18-34]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools



Advisory comments

4. Unused webhook secret envvar 🐞 Bug ⚙ Maintainability
Description
.env.example adds TELEGRAM_WEBHOOK_SECRET, but the bot code runs in polling mode and no
application code reads this secret. This is misleading configuration that will drift or confuse
deploys.
Code

.env.example[R12-13]

Evidence
The example env file introduces the secret, but the Telegram bot uses run_polling() and no Python
files reference TELEGRAM_WEBHOOK_SECRET, so the configuration is currently unused.

.env.example[10-13]
app/services/telegram_bot.py[70-83]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
`TELEGRAM_WEBHOOK_SECRET` is documented in `.env.example` but is not used anywhere in code; the Telegram integration uses polling.

## Issue Context
- `app/services/telegram_bot.py` starts polling via `run_polling()` and does not implement webhook request validation.

## Fix
Either:
- Remove `TELEGRAM_WEBHOOK_SECRET` from `.env.example` until webhook support is implemented, or
- Implement webhook mode + verification and then keep the env var.

## Fix Focus Areas
- .env.example[11-13]
- app/services/telegram_bot.py[70-83]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Grey Divider

Qodo Logo

Comment on lines +4 to +6
from dotenv import load_dotenv
from telegram import Update
from telegram.ext import ApplicationBuilder, CommandHandler, MessageHandler, ContextTypes, filters

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Action required

1. Missing telegram dependency 🐞 Bug ≡ Correctness

app/services/telegram_bot.py imports the telegram package, but the project dependency list does
not include python-telegram-bot, so importing/running the bot will fail with
ModuleNotFoundError. This blocks the Telegram integration from working at all.
Agent Prompt
## Issue description
`app/services/telegram_bot.py` imports `telegram` / `telegram.ext`, but the repository does not declare a dependency that provides those modules. This will crash at import-time with `ModuleNotFoundError: No module named 'telegram'`.

## Issue Context
- The Telegram bot implementation lives in `app/services/telegram_bot.py`.
- Dependencies are declared in `pyproject.toml`; lockfile should be updated accordingly.

## Fix
1. Add `python-telegram-bot` to `[project].dependencies` in `pyproject.toml`.
2. Re-lock dependencies (e.g., `uv add python-telegram-bot` or equivalent) so `uv.lock` is updated.

## Fix Focus Areas
- pyproject.toml[1-13]
- app/services/telegram_bot.py[1-13]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools

@Itaypk Itaypk merged commit 4d6dd79 into main May 24, 2026
4 checks passed
@Itaypk Itaypk deleted the adding-telegram-input branch May 24, 2026 17:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants